package com.example.ck.controller;

import com.example.ck.service.UserService;
import com.example.ck.utils.AliOSSUtils;
import com.example.ck.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@Slf4j
@RestController
@RequestMapping("/upload")
@CrossOrigin
public class UploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/image")
    public ResultVo upload(MultipartFile file) throws IOException {
        log.info("文件上传,文件名:{}", file.getOriginalFilename());
        // 调用阿里云工具类
        String url = aliOSSUtils.uploadImg(file);
        ResultVo vo = new ResultVo<>();
        vo.setCode(200);
        vo.setMessage("图片上传成功");
        vo.setSuccess(true);
        vo.setData(url);
        return vo;
    }

    @PostMapping("/document")
    public ResultVo uploadDoc(MultipartFile doc) throws IOException {
        log.info("文件上传,文件名:{}", doc.getOriginalFilename());
        // 调用阿里云工具类
        String url = aliOSSUtils.uploadDoc(doc);
        ResultVo vo = new ResultVo<>();
        vo.setCode(200);
        vo.setMessage("文件上传成功");
        vo.setSuccess(true);
        vo.setData(url);
        return vo;
    }
}
